package com.chj.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.chj.constants.Constants;
import com.chj.dao.UserDAO;
import com.chj.entity.User;
import com.chj.mapper.UserMapper;
import com.etc.jdbc.util.JDBCUtils;

@Repository
public class UserDAOImpl implements UserDAO {

	
	@Override
	public User get(String userId, String userPassword) {

		String sql = "";

		sql += " SELECT *                   ";
		sql += "   FROM t_user T            ";
		sql += "  WHERE T.user_id       = ? ";
		sql += "    AND T.user_password = ? ";
	
		return JDBCUtils.queryForObject(sql, new UserMapper(), userId, userPassword);
	}

	@Override
	public boolean checkUserIdExist(String userId) {

		String sql = "";

		sql += " select count(1)      ";
		sql += "   from t_user T      ";
		sql += "  where T.user_id = ? ";
		
		Map<String, Object> queryForMap = JDBCUtils.queryForMap(sql, userId);
		Long size = (Long) queryForMap.get("count(1)");

		if (size.intValue() > 0) {

			return true;
		} else {
			
			return false;
		}
	}

	@Override
	public boolean addUser(User user) {
		
		String sql = "";
		
		sql += " INSERT                                                                                                                                     ";
		sql += "   INTO t_user                                                                                                                              ";
		sql += " (user_pk, user_id, user_name, user_password, user_type, user_status, user_phone, user_email, create_date, modify_date, delete_date, status)";
		sql += " VALUES                                                                                                                                     ";
		sql += " (?      ,?      , ?        , ?            , ?        , ?          , ?         , ?         , ?          , ?          , ?          , ?     )";
		
		int i = JDBCUtils.update(sql
						       , user.getUserPk()
							   , user.getUserId()
							   , user.getUserName()
							   , user.getUserPassword()
							   , user.getUserType()
							   , user.getUserStatus()
							   , user.getUserPhone()
							   , user.getUserEmail()
							   , new Date()
							   , Constants.NULL_DATE
							   , Constants.NULL_DATE
							   , Constants.ROW_STATUS_EFFECTIVE);
		
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	public User getAdmin(String userId, String userPassword){
	    String sql = "";

        sql += " SELECT *                   ";
        sql += "   FROM t_user T            ";
        sql += "  WHERE T.user_id       = ? ";
        sql += "    AND T.user_password = ? ";
        sql += "    AND T.user_type     = ? ";
    
        return JDBCUtils.queryForObject(sql, new UserMapper(), userId, userPassword,2);
	}

    @Override
    public List<User> getUserListBy(String userName, String userPhone, String userEmail, String dateBegin, String dateEnd, int userType, int page, int rows) {

        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select *                        ";
        sql += "   from t_user t                 ";
        sql += "  where t.user_type = ?          ";

        paramsList.add(userType);

        if (StringUtils.isNotBlank(userName)) {
            sql += " and t.user_name like ? ";
            paramsList.add(userName+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(userPhone)) {
            sql += " and t.user_phone like ? ";
            paramsList.add(userPhone + Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(userEmail)) {
            sql += " and t.user_email like ? ";
            paramsList.add(userEmail+Constants.SQL_LIKE);
        }
        if (StringUtils.isNotBlank(dateBegin)) {
            sql += " and t.create_date > ? ";
            paramsList.add(dateBegin);
        }
        if (StringUtils.isNotBlank(dateEnd)) {
            sql += " and t.create_date < ? ";
            paramsList.add(dateEnd);
        }

        sql += " limit ?,?";

        paramsList.add(((page-1)*rows));
        paramsList.add((page)*rows);

        //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForList(sql, new UserMapper(), params);
    }

    @Override
    public int getUserListSizeBy(String userName, String userPhone,String userEmail, String dateBegin, String dateEnd, int userType) {

        List<Object> paramsList = new ArrayList<Object>();

        String sql = "";

        sql += " select count(1)                 ";
        sql += "   from t_user t                 ";
        sql += "  where t.user_type = ?          ";

        paramsList.add(userType);

        if (StringUtils.isNotBlank(userName)) {
            sql += " and t.user_name like ? ";
            paramsList.add(userName+Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(userPhone)) {
            sql += " and t.user_phone like ? ";
            paramsList.add(userPhone + Constants.SQL_LIKE);
        }

        if (StringUtils.isNotBlank(userEmail)) {
            sql += " and t.user_email like ? ";
            paramsList.add(userEmail+Constants.SQL_LIKE);
        }
        if (StringUtils.isNotBlank(dateBegin)) {
            sql += " and t.create_date > ? ";
            paramsList.add(dateBegin);
        }
        if (StringUtils.isNotBlank(dateEnd)) {
            sql += " and t.create_date < ? ";
            paramsList.add(dateEnd);
        }

      //定义数组长度
        int paramsLength = 0;
        if(paramsList.size() > 0) {
            paramsLength = paramsList.size();
        }

        //创建数组
        Object[] params = new Object[paramsLength];

        //赋值给数组
        for (Object object : paramsList) {
            int paramsIndex = paramsList.indexOf(object);
            params[paramsIndex] = object;
        }

        return JDBCUtils.queryForInt(sql, params);
    }

    @Override
    public int update(User user) {

        String sql = "UPDATE `t_user` SET `user_name`=?, `user_password`= ?, `user_phone`= ? , `user_email`= ?,`modify_date`=? WHERE (`user_pk`= ? and `user_type`=?)";
        return JDBCUtils.update(sql, user.getUserName(), user.getUserPassword(), user.getUserPhone(), user.getUserEmail(),new Date(),user.getUserPk(), user.getUserType());
    }

    @Override
    public User getUserByUserPk(String userPk) {
        String sql = "select * from t_user where user_pk = ?";
        return JDBCUtils.queryForObject(sql, new UserMapper(), userPk);
    }
}
